from django.db import models
from apps.common.models import CommonItem
#from feedbag.apps.review.models import Review

class Brand(CommonItem):
	name = models.CharField(max_length=90)
	slug = models.SlugField(max_length=40)
	logo = models.FileField(upload_to='uploads/brands/')
	description = models.TextField()
	
	def __unicode__(self):
		return self.name
	
	@models.permalink
	def get_absolute_url(self):
		return ('brand_view', (), {'slug_brand': self.slug})
	
class ProductType(models.Model):
	name = models.CharField(max_length=90)
	slug = models.SlugField(max_length=40)
	
	def __unicode__(self):
		return self.name

class Product(CommonItem):
	brand = models.ForeignKey(Brand)
	type = models.ForeignKey(ProductType)
	name = models.CharField(max_length=90)
	slug = models.SlugField(max_length=40)
	cost = models.CharField(max_length=10)
	weight = models.CharField(max_length=10)
	image = models.FileField(upload_to='uploads/products/')
	description = models.TextField()
	has_review = models.BooleanField(default = False)

	def reviews(self):
	   return Review.objects.all().filter(active=True).filter(product=self.product)
   
	def total_reviews(self):
	   return len(self.reviews())

	def nutrition(self):
		return ProductNutrition.objects.get(product=self)
	
	@models.permalink
	def get_absolute_url(self):
		return ('product_view', (), {'slug_brand': self.brand.slug, 'slug_product': self.slug})
	
	def __unicode__(self):
		return self.name

class ProductNutrition(models.Model):
	product = models.OneToOneField(Product)
	fat = models.DecimalField(max_digits=4, decimal_places=2)
	protein = models.DecimalField(max_digits=4, decimal_places=2)
	carbohydrate = models.DecimalField(max_digits=4, decimal_places=2)
	sodium = models.DecimalField(max_digits=4, decimal_places=2)
	
	def __unicode__(self):
		return str(self.id)

class ProductImage(models.Model):
	product = models.ForeignKey(Product)
	image = models.FileField(upload_to='uploads/brands/')
	
	def __unicode__(self):
		return self.image

class Review(CommonItem):
	product = models.ForeignKey(Product)
	body = models.TextField()
	rating_taste = models.DecimalField(max_digits=2, decimal_places=1)
	rating_value = models.DecimalField(max_digits=2, decimal_places=1)
	rating_performance = models.DecimalField(max_digits=2, decimal_places=1)
	
	def rating_overall(self):
		return (self.rating_taste + self.rating_value + self.rating_performance) // 3
	
	def __unicode__(self):
		return str(self.id)